package com.ibm.workplace.wcm.cqplugin;

import org.eclipse.jface.viewers.*;
import org.eclipse.swt.*;
import org.eclipse.swt.graphics.*;
import org.eclipse.swt.widgets.*;

import com.ibm.collaboration.realtime.people.*;

public class BugInfoLabelProvider extends LabelProvider implements ITableLabelProvider, ITableColorProvider, ITableFontProvider {

    public BugInfoLabelProvider() {
        super();
    }

    public Image getColumnImage(Object arg0, int col) {
        IBugInfo bugInfo = (IBugInfo) arg0;

        if (col == 6) {
            if (bugInfo.getOriginatorEmailAddress() != null && !bugInfo.getOriginatorEmailAddress().trim().equals("")) {
                /*kevin
            	Person p = BugInfoPluginActivator.getPerson(bugInfo.getOriginatorEmailAddress());
                if (p != null) {
                    return p.getStatusImage();
                }*/
            }
        } else if (col == 7) {
            if (bugInfo.getOwnerEmailAddress() != null && !bugInfo.getOwnerEmailAddress().trim().equals("")) {
            	 /*kevin
            	Person p = BugInfoPluginActivator.getPerson(bugInfo.getOwnerEmailAddress());
                if (p != null) {
                    return p.getStatusImage();
                }*/
            }            
        } /*kevin else if (col == 2 && bugInfo.getPriority() != null) {
            if (bugInfo.getPriority().equals(BugPriority.HIGH)) {
                return BugInfoPluginActivator.getImage("icons/priority_high.gif");
            } else if (bugInfo.getPriority().equals(BugPriority.MEDIUM)) {
                return BugInfoPluginActivator.getImage("icons/priority_medium.gif");
            } else if (bugInfo.getPriority().equals(BugPriority.LOW)) {
                return BugInfoPluginActivator.getImage("icons/priority_low.gif");
            } else {
                return BugInfoPluginActivator.getImage("icons/priority_unassign.gif");
            }
        } else if (col == 3 && bugInfo.getSeverity() != null) {
            if (bugInfo.getSeverity().equals(BugSeverity.ONE)) {
                return BugInfoPluginActivator.getImage("icons/sev_major.gif");
            } else if (bugInfo.getSeverity().equals(BugSeverity.TWO)) {
                return BugInfoPluginActivator.getImage("icons/sev_normal.gif");
            } else if (bugInfo.getSeverity().equals(BugSeverity.THREE)) {
                return BugInfoPluginActivator.getImage("icons/sev_normal.gif");
            } else {
                return BugInfoPluginActivator.getImage("icons/sev_minor.gif");
            }
        }*/
        
        //return BugInfoPluginActivator.getImage(BugInfoPluginActivator.IMAGE_ADD_COMMENT);
        return null;
    }

    public String getColumnText(Object arg0, int arg1) {
        if (arg0 instanceof IBugInfo) {
            IBugInfo bugInfo = (IBugInfo) arg0;

            String ret = null;
            switch (arg1) {
            case 0:
                ret = bugInfo.getId();
                break;
            case 1:
                ret = bugInfo.getState() != null ? bugInfo.getState().toString() : "";
                break;
            case 2:
                ret = bugInfo.getSeverity() != null ? bugInfo.getSeverity().toString() : "";
                break;
            case 3:
                ret = bugInfo.getDescription();
                break;
            case 4:
                ret = bugInfo.getTestType();
                break;
            case 5:
                ret = bugInfo.getOriginatorId();
                
                if (bugInfo.getOriginatorEmailAddress() != null) {                
                    /*kevin
                	Person p = BugInfoPluginActivator.getPerson(bugInfo.getOriginatorEmailAddress());
                    if (p != null) {
                        // TODO: At some point, we should see if "p" matches the user currently signed into ST
                        if (p.getDisplayName().equals("Will1111")) {
                            ret = "Me";
                        } else {
                            ret = p.getDisplayName();
                        }
                    }*/
                }
                break;
            case 6:
                ret = bugInfo.getOwnerId();
                
                if (bugInfo.getOwnerEmailAddress() != null) {
                	/*kevin
                    Person p1 = BugInfoPluginActivator.getPerson(bugInfo.getOwnerEmailAddress());
                    if (p1 != null) {
                        if (p1.getDisplayName().equals("Will1111")) {
                            ret = "Me";
                        } else {
                            ret = p1.getDisplayName();
                        }
                    }*/
                }
                break;
            }
            
            return (ret != null ? ret : "");
        } else {
            return "";
        }
    }

    public Color getBackground(Object arg0, int arg1) {
        if (arg0 instanceof IBugInfo) {
            if (((IBugInfo)arg0).getId().equals("36477")) {
              //  return new Color(Display.getDefault(), new RGB(220, 255, 220));
            }
        }
        
        //kevin return null;
        return new Color(Display.getDefault(), new RGB(220, 255, 220));
    }

    public Color getForeground(Object arg0, int column) {
        // TODO: This is for testing only ... eventually colors should come through preferences
        
        if (arg0 instanceof IBugInfo) {
            IBugInfo bugInfo = (IBugInfo) arg0;
            if (bugInfo.getState() != null && bugInfo.getState().equals(BugState.CLOSED)) {
                return Display.getDefault().getSystemColor(SWT.COLOR_DARK_GRAY);
            }
            
            // highlight "high" priority defects in red (but don't color owner or originator columns)
            if (bugInfo.getPriority() != null && bugInfo.getPriority().equals(BugPriority.HIGH) && column <= 4) {
                return Display.getDefault().getSystemColor(SWT.COLOR_RED);
            }
        } 
        
        return null;        
    }
    
    private Font boldFont = null;
    private Font italicsFont = null;

    public Font getFont(Object arg0, int column) {
        // TODO: Need a "FontManager" class
        if (arg0 instanceof IBugInfo) {
            IBugInfo bugInfo = (IBugInfo)arg0;
            if (bugInfo.getPriority() != null && bugInfo.getPriority().equals(BugPriority.HIGH) && column <= 4) {
                if (boldFont == null) {
                    //boldFont = Fontman new Font(Display.getDefault(), Display.getDefault().getSystemFont().getFontData()[0].getName(), 8, SWT.BOLD);
                }
                
                return boldFont;
            }
            if (bugInfo.getPriority() != null &&((IBugInfo)arg0).getPriority().equals(BugPriority.LOW)) {
                //if (italicsFont == null) {
                //    italicsFont = new Font(Display.getDefault(), Display.getDefault().getSystemFont().getFontData()[0].getName(), 8, SWT.ITALIC);
                //}
                
             //   return italicsFont;                
            }
        }
        
        return null;
    }
    
}
